<!-- 操作日志 -->
<template>
  <div
    class="operation_log"
    v-if="!appStore.isPhone">
    <SearchCard
        ref="searchCardRef"
        :searchTemplate="searchTemplate"
        :more="false"
        @reset="getList"
        @search="(data) => getList(data)" />
    <el-card class="table_card">
      <Table
        :data="tableData"
        v-loading="tableLoading"
        :height="height">
        <el-table-column
          prop="userName"
          label="用户名"
          min-width="110"
          :resizable="false"
        />
        <el-table-column
          prop="info"
          label="操作信息"
          min-width="200"
          :resizable="false"
        />
        <el-table-column
          prop="requestUrl"
          label="访问路径"
          min-width="150"
          :resizable="false"
        />
        <el-table-column
          prop="ipAddress"
          label="ip地址"
          min-width="130"
          :resizable="false"
        />
        <el-table-column
          prop="deviceInfo"
          label="用户端设备信息"
          min-width="100"
          :resizable="false"
        />
        <el-table-column
          prop="createTime"
          label="操作时间"
          min-width="220"
          :resizable="false">
          <template #default="scope">
            {{ formatDateTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          min-width="100"
          :resizable="false">
          <template #default="scope">
            <el-button
              type="success"
              @click="viewEvent(scope.row)">
              {{ config.button.text.view }}
            </el-button>
          </template>
        </el-table-column>
      </Table>
      <Pagination
        :pageNum="pageNum"
        :pageSize="pageSize"
        :total="total"
        @handle-size-change="(val) => pageSize = val"
        @handle-current-change="(val) => pageNum = val" />
    </el-card>
    <!-- 查看 -->
    <View
      ref="viewRef"
      :rowData="rowData" />
  </div>
  <!-- 手机端页面 -->
  <H5Index v-else></H5Index>
</template>

<script setup>
import { ref, onMounted, watch  } from "vue";
import Pagination from "@/components/Pagination.vue"
import Table from "@/components/Table.vue"
import ApiOperationLog from "@/api/modules/operation-log"
import { useTable } from "@/hooks/useTable"
import View from "./component/View.vue"
//import Edit from "./component/edit.vue"
// import message from "@/common/element-plus/message";
import config from "@/common/element-plus/config";
import app from "@/common/config/app";
import { useAppStore } from "@/stores/app"
import { storeToRefs } from "pinia";
import { formatDateTime } from "@/common/utils/date";
import H5Index from "./h5Index.vue";
import SearchCard from "@/components/SearchCard.vue"


// 查询表单
const formData = ref({})
// 查看组件实例
const viewRef = ref()

// 表格每行数据
const rowData = ref({})
// 表格高度(固定表头)
const height = ref(0)
// 查询卡片实例
const searchCardRef = ref()

const selectTime = ref([]);

// 查询模板
const searchTemplate = ref([
  {
    label: "操作信息",
    type: "input",
    placeholder: "操作信息",
    value: "info",
    isClosed: false,
  },
  {
    label: "操作日期",
    type: "daterange",
    value: "selectTime",
    isClosed: false,
  },
]);

const { tableMethods, tableState, pagination } = useTable({
  fetchDataApi: async (data) => {
    data={...data,isFinishPick: false}
    const { pageSize, pageNum } = pagination
    const res = await ApiOperationLog.selectPage(data, {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
    })
    console.log(res)
    return {
      list: res.data.records,
      total: res.data.total
    }
  }
})
const { getList } = tableMethods
const { pageSize, pageNum } = pagination
const { total, tableLoading, tableData } = tableState

// 监听时间选择
watch(() => selectTime.value, (newVal) => {
  if (newVal) {
    if (Array.isArray(newVal)&&newVal.length===2){
      formData.value.startTime = newVal[0];
      formData.value.endTime = newVal[1];
    } else {
      formData.value.startTime="";
      formData.value.endTime="";
    }
  }
}, { immediate: true })

// 重置
const reset = () => {
  formData.value = {}
  selectTime.value = []
  getList()
}

// 打开查看对话框
const viewEvent = (row) => {
  viewRef.value.visibility = true
  rowData.value = row
}

// 监听查询卡片大小变化
const resizeObserver = new ResizeObserver(entries => {
  for (const entry of entries) {
    if (contentFullScreen.value) {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - entry.contentRect.height) + "px"
    } else {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - app.headerHeight - entry.contentRect.height) + "px"
    }
  }
})

const appStore = useAppStore()

// 保持响应式
const { contentFullScreen } = storeToRefs(appStore)

onMounted(() => {
  if (appStore.isPhone) return
  resizeObserver.observe(searchCardRef.value.$el)
})
</script>

<style lang="scss" scoped>
.operation_log {
  .search_card {
    margin-bottom: 4px;

    :deep(.el-form-item__label) {
      min-width: 80px;
    }

    :deep(.el-card__body) {
      padding: 10px;
      display: flex;
      justify-content: space-between;
    }

    .el-button {
      margin: 5px 10px 5px 0;
    }

    .query_button {
      margin-left: 100px;
    }

    .el-form-item {
      margin-right: 10px;
      padding: 5px 0;
    }
  }

  .table_card {
    :deep(.el-card__body) {
      padding: 0;
    }
  }

}
</style>
